package com.example.demo.Controller;

import com.example.demo.Mapper.CardMapper;
import com.example.demo.entity.CardInfo;
import com.example.demo.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
public class Card {
    @Autowired
    CardMapper cardMapper;

    @RequestMapping("/card")
    public String cardInfo(Model model)
    {
        List<CardInfo> results=cardMapper.getCardsInfo();
        model.addAttribute("results",results);
        return "/manager/CardViewer.html";
    }

    @RequestMapping("/applyCard")
    public String clientAdd(Model model,
                            @RequestParam(value = "card_id")int card_id,
                            @RequestParam(value = "client_id")int client_id,
                            @RequestParam(value = "balance")double balance,
                            @RequestParam(value = "bank_id")int bank_id,
                            @RequestParam(value = "card_type")String card_type,
                            @RequestParam(value = "time") String time)
    {
        if(card_type == "credit_card")
        {
            int flag1=cardMapper.addCardInfo(card_id,client_id,balance,bank_id,card_type,time);
            int flag2=cardMapper.addCreditCardInfo(card_id);
            int flag3=cardMapper.addApply(card_id,client_id,time);

            if(flag1 == 1 && flag2 == 1 && flag3 == 1)
            {
                List<CardInfo> results=cardMapper.getCardsInfo();
                model.addAttribute("results",results);
                return "/manager/CardViewer.html";
            }
            else {
                return "/error";
            }
        }
        else
        {
            int flag1=cardMapper.addCardInfo(card_id,client_id,balance,bank_id,card_type,time);
            int flag2=cardMapper.addSavingCardInfo(card_id);
            int flag3=cardMapper.addApply(card_id,client_id,time);

            if(flag1 == 1 && flag2 == 1 && flag3 == 1)
            {
                List<CardInfo> results=cardMapper.getCardsInfo();
                model.addAttribute("results",results);
                return "/manager/CardViewer.html";
            }
            else {
                return "/error";
            }
        }
    }
    @RequestMapping("/updateCard")
    public String clientUpdate(Model model,
                               @RequestParam(value = "card_id")int card_id,
                               @RequestParam(value = "balance")double balance,
                               @RequestParam(value = "bank_id")int bank_id,
                               @RequestParam(value = "card_type")String card_type)
    {
        if(card_type == "credit_card")
        {
            int flag1=cardMapper.updateCardInfo(card_id,balance,bank_id);
            int flag2=cardMapper.updateCreditCardInfo(card_id);

            if(flag1 == 1 && flag2 == 1 )
            {
                List<CardInfo> results=cardMapper.getCardsInfo();
                model.addAttribute("results",results);
                return "/manager/CardViewer.html";
            }
            else {
                return "/error";
            }
        }
        else
        {
            int flag1=cardMapper.updateCardInfo(card_id,balance,bank_id);

            if(flag1 == 1 )
            {
                List<CardInfo> results=cardMapper.getCardsInfo();
                model.addAttribute("results",results);
                return "/manager/CardViewer.html";
            }
            else {
                return "/error";
            }
        }

    }
 /*  @RequestMapping("/deletecard")
    public String  delect_card(String card_id)
    {
        cardMapper.delectcard(card_id);
        return "/manager/CardViewer.html";
    }*/
}
